package Chat;

// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import Entity.*;
/*import java.sql.ResultSet;
import java.text.Normalizer.Form;
import java.util.Iterator;
import java.util.Timer;


import Chat.ScheduledTask;
import Entity.*;*/
//import java.util.ArrayList;

import ocsf.server.*;

/**
 * This class overrides some of the methods in the abstract superclass in order
 * to give more functionality to the server.
 * 
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Paul Holden
 * @version July 2000
 */
public class EchoServer extends AbstractServer {

	//private Timer timer;
	//private ScheduledTask onceADayTask;

//	private Timer timer;
	//private ScheduledTask onceADayTask;

	private mysqlConnection sqlc;
	private Object result;

	//Constructors ****************************************************

	/**
	* Constructs an instance of the server.
	*
	* @param port The port number to connect on.
	*/
	public EchoServer(int port) 
	{
		super(port);
		
		sqlc=new mysqlConnection();
		result=null;

	}


	//Instance methods ************************************************

	/**
	* This method handles any messages received from the client.
	* It redirects it to the database class and receives an answer as
	* an object and sends it back to client
	*
	* @param msg The object message received from the client.
	* @param client The connection from which the message originated.
	*/
		public void handleMessageFromClient(Object msg, ConnectionToClient client) {
			MsgPackage new_msg= (MsgPackage)msg;
			
			switch (new_msg.getOp_code()){
			case LOGIN:
				User u = (User) new_msg.getObject();
				result=sqlc.loginQuery(u);
				break;
			case UPDATE_WORKER_PASSWORD:
				User u1 = (User) new_msg.getObject();
				result = sqlc.changePassQuery(u1);
				break;
			case ADD_NEW_WORKER:
				User u2 = (User) new_msg.getObject();
				result = sqlc.addWorkerQuery(u2);
				break;
			case REMOVE_WORKER:
				String wID = (String) new_msg.getObject();
				result = sqlc.removeWorkerQuery(wID);
				break;
			case GET_WORKERS_NAMES:
				result = sqlc.getWorkersNameQuery();
				break;
			case GET_WORKER_LAST_NAME:
				String wFname = (String) new_msg.getObject();
				result = sqlc.getWorkerLastNameQuery(wFname);
				break;
			case GET_WORKER_ID:
				String name = (String) new_msg.getObject();
				result = sqlc.getWorkerIDQuery(name);
				break;
			case GET_WORKER_PER:
				Integer wId = (Integer) new_msg.getObject();
				result = sqlc.getWorkerPermissionQuery(wId);
				break;
			case UPDATE_WORKER_DETAILS:
				User u3 = (User) new_msg.getObject(); 
				result = sqlc.updateworkerdetailsQuery(u3);
				break;
			case GET_CUSTOMER_DETAILS:
				Integer CustID = (Integer) new_msg.getObject(); 
				result = sqlc.viewCustomerDetailsQuery(CustID);
				break;
			case GET_ALL_CUSTOMER:
				result = sqlc.getAllCustomerDetailsQuery();
				break;
			case CLOSE_EVENT:
				Event ev = (Event) new_msg.getObject(); 
				result = sqlc.closeEventQuery(ev);
				break;
			case GET_ALL_CUSTOMER_MEETING:
				result = sqlc.getCustomersMeetingQuery();
				break;
			case IS_EVENT_EXISTS:
				Event event = (Event) new_msg.getObject();
				result = sqlc.isEventExistQuery(event);
				break;
			case IS_EVENT_OPEN:
				Event event1 = (Event) new_msg.getObject();
				result = sqlc.isEventOpenQuery(event1);
				break;
			case GET_WORKER_TO_SCHEDULE:
				String date = (String) new_msg.getObject();
				result = sqlc.getWorkerToScheduleQuery(date);
				break;
			case SET_SCHEDULE:
				Schedule sc = (Schedule) new_msg.getObject();
				result = sqlc.setScheduleQuery(sc);
				break;
			case CREATE_EVENT:
				Event event2 = (Event) new_msg.getObject();
				result = sqlc.createEventQuery(event2);
				break;
			case CANCEL_EVENT:
				Event eve = (Event) new_msg.getObject();
				result = sqlc.cancelEventQuery(eve);
				break;
			case EDIT_EVENT:
				Event e1 = (Event) new_msg.getObject();
				result = sqlc.editEventQuery(e1);
				break;
			case COORDINATING_MEETING:
				Meeting m = (Meeting) new_msg.getObject();
				result = sqlc.coordinatingMeetingQuery(m);
				break;
			case ADD_CUSTOMER:
				Customer c = (Customer) new_msg.getObject();
				result = sqlc.addCustomerQuery(c);
				break;
			case GET_EMAIL:
				E_Mail email = (E_Mail) new_msg.getObject();
				result = sqlc.getE_Mail(email);
				break;
			case GET_NUM_OF_EVENTS:
				Integer cID = (Integer) new_msg.getObject();
				result = sqlc.getCustomerNumOfEventsQuery(cID);
				break;
			case VIEW_WORKER_DETAILS:
				Integer workerId = (Integer) new_msg.getObject();
				result = sqlc.getWorkerDetailsQuery(workerId);
				break;
			case SET_PERMISSION:
				User u4 = (User) new_msg.getObject();
				result = sqlc.setPermissionLevelQuery(u4);
				break;
			case GET_DATE_LIST:
				result = sqlc.getEventDatesQuery();
				break;
			case GET_MANAGER_NAME:
				result = sqlc.getManagersNameQuery();
				break;
			case CHANGE_MANAGER_INCHARGE:
				Meeting meet = (Meeting)new_msg.getObject();
				result = sqlc.changeManagersInchargeQuery(meet);
				break;			
			case GET_CIDS:
				result = sqlc.getCidsQuery();
				break;
			case GET_MEETING:
				result = sqlc.getMeetingQuery();
				break;
			case VIEW_EVENT:
				Event event3 = (Event) new_msg.getObject();
				result = sqlc.getEventDetailsQuery(event3);
				break;
			case UPDATE_CUSTOMER_DETAILS:
				Customer customer = (Customer) new_msg.getObject();
				result = sqlc.updateCustomerDetailsQuery(customer);
				break;
			case REMOVE_CUSTOMER:
				Customer cst = (Customer) new_msg.getObject();
				result = sqlc.removeCustomerQuery(cst);
				break;
			case EDIT_MEETING:
				Meeting mToEdit = (Meeting) new_msg.getObject();
				result = sqlc.editMeetingQuery(mToEdit);
				break;
			case VIEW_SCHEDULE:
				result = sqlc.getScheduleQuery();
				break;
			case GET_DATES:
				result = sqlc.getDateToScheduled();
				break;
			case GET_EVENT_PLACES:
				String placeBYdate = (String) new_msg.getObject();
				result = sqlc.getEventPlaces(placeBYdate);
				break;
			case GET_MSG:
				Integer mID = (Integer) new_msg.getObject();
				result = sqlc.getMeetingMassage(mID);
				break;
			case GET_ALL_USERS:
				result = sqlc.getAllUsersQuery();
				break;
			case GET_EVENT_PER_MONTH:
				Report ePerMonth = (Report) new_msg.getObject();
				result = sqlc.getEventInMonthQuery(ePerMonth);
				break;
			case GET_EVENT_TO_SCHEDULE:
				result = sqlc.getEvenToScheduleQuery();
				break;
			case GET_HOLES_PER_MONTH:
				Report r = (Report) new_msg.getObject();
				result = sqlc.getEventPerHolesQuery(r);
				break;
			case GET_WORKER_EMAIL:
				WorkerEmails we = (WorkerEmails) new_msg.getObject();
				result = sqlc.getWorkersE_Mail(we);
				break;
			case VERIFY_RANDOM_PASS:
				String pass = (String) new_msg.getObject();
				result = sqlc.getVerifyPassQuery(pass);
				break;
			case SAVE_VIDEO:
				Event vPath = (Event) new_msg.getObject();
				result = sqlc.saveVideoPathQuery(vPath);
				break;
			case GET_VIDEO:
				Event gvPath = (Event) new_msg.getObject();
				result = sqlc.getVideoPathQuery(gvPath);
				break;
			default:
			}
			
			
			//this.sendToAllClients(result);
			try {
				client.sendToClient(result);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
}

// Class methods ***************************************************



//End of EchoServer class

		